package com.leoren.run.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author Leoren
 * @Date 2018/6/27 20:03
 *
 * 时间的格式化工具类
 */
public class DateParseUtil {

    /**
     * 获取时间 小时:分;秒 HH:mm:ss
     *
     * @return
     */
    public static String parseData(Date date){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    public static String parseDataHM(Date date){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String str = format.format(date);
        return str;
    }

    public static String parseDataHMS(Date date){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String str = format.format(date);
        return str;
    }

    public static String parseDataYMD(Date date){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    public static String dateToString(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        return df.format(date);
    }

    public static String getChattime(Date date){
        DateFormat he = DateFormat.getTimeInstance();
        return he.format(date).toString();
    }


    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShort(Date currentTime) {
        //Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间的月份
     * @return
     */
    public static int getMonth(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.MONTH);
    }

    public static int getYear(){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    public static int getIntDate(String str){
        String[] strings = str.split(":");
        int hour = Integer.parseInt(strings[0]);
        int min = Integer.parseInt(strings[1]);
        int second = Integer.parseInt(strings[2]);
        return hour*3600 + min * 60 + second;
    }


    public static String GetUsedTime(int usedTime) {
        int hour = usedTime / 60;
        int min = usedTime % 60;
        return new StringBuilder().append(hour).append("时").append(min).append("分").toString();

    }

    /**
     * 获取明天的时期  xxxx-xx-xx格式
     * @return
     */
    public static String GetTomorrow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 1);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
        String time = sdf.format(c.getTime());
        return time;
    }

    public static String GetToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 0);//-1.昨天时间 0.当前时间 1.明天时间 *以此类推
        String time = sdf.format(c.getTime());
        return time;
    }

    public static Date GetTodayDate() {
        Calendar calendar = Calendar.getInstance();
        return new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    }


    public static Date GetTomorrowDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);//-1.
        return new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    }

    public static Date GetYesterdayDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);//-1.
        return new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    }

    public static Date stringToDate(String lastChatTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
        try {
            return format.parse(lastChatTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean isSameDay(Date date, Date now){
        if(date != null && now != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(now);
            return isSameDay1(cal1, cal2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isSameDay1(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static String dateFromIntMin(Integer time) {
        int second = time % 60;
        int min = time / 60;
        int hour = time / 3600;
        StringBuilder sb = new StringBuilder();
        if(hour > 0 && hour < 10){
            sb.append("0").append(hour);
        }else if(hour > 0){
            sb.append(hour);
        }else {
            sb.append("00");
        }
        sb.append(":");
        if(min > 0 && min < 10){
            sb.append("0").append(min);
        }else if(min > 0){
            sb.append(min);
        }else {
            sb.append("00");
        }
        sb.append(":");
        if(second > 0 && second < 10){
            sb.append("0").append(second);
        }else if(second > 0){
            sb.append(second);
        }else {
            sb.append("00");
        }
        return sb.toString();
    }
}
